package com.x.wx.dao;


import com.alibaba.dubbo.common.utils.StringUtils;
import com.x.framework.dao.BaseDao;
import com.x.wx.model.WxUser;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class WxUserDao  extends BaseDao{

    private final static  String  selectWxUserListSql = "SELECT T1.* FROM WX_USER T1 WHERE 1=1 ";
    /**
     * 查询微信用户
     * @param wxUser
     * @return
     * @throws Exception
     */

    public List<WxUser> selectWxUserList(WxUser wxUser) throws Exception{
        StringBuilder sql = new StringBuilder(selectWxUserListSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(sql, params, wxUser);
        return this.queryForList(sql.toString(), WxUser.class, params, wxUser);
    }

    private final static  String  selectWxUserCountSql = "SELECT COUNT(T1.USER_ID) FROM WX_USER  T1 WHERE 1=1 ";
    /**
     * 查询微信用户
     * @param wxUser
     * @return
     * @throws Exception
     */

    public int selectWxUserNum(WxUser wxUser) throws Exception{
        StringBuilder sql = new StringBuilder(selectWxUserCountSql);
        List<Object> params = new ArrayList<Object>();
        searchCondition(sql, params, wxUser);
        return this.queryForInt(sql.toString(), params);
    }


    /**
     * 查询条件
     * @param sql
     * @param params
     * @param wxUser
     */
    private void searchCondition(StringBuilder sql, List<Object> params, WxUser wxUser) {
        if(wxUser != null) {
            if(StringUtils.isNotEmpty(wxUser.getUserId())){
                sql.append("AND T1.USER_ID = ? ");
                params.add(wxUser.getUserId());
            }
            if(StringUtils.isNotEmpty(wxUser.getUserOpenId())){
                sql.append("AND T1.USER_OPEN_ID = ? ");
                params.add(wxUser.getUserOpenId());
            }
            if( wxUser.getUserStatus() != null){
                sql.append("AND T1.USER_STATUS = ? ");
                params.add(wxUser.getUserStatus());
            }
            if( wxUser.getAccountId() != null){
                sql.append("AND T1.ACCOUNT_ID = ? ");
                params.add(wxUser.getAccountId());
            }
        }
    }

}
